//
//  CZBaseViewController.swift
//  CY--weiBo
//
//  Created by mac on 15/11/23.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class CZBaseViewController: UITableViewController {

 /// 用户登录状态
    var userlogin = CZUserAccount.userLogin
    
    override func loadView() {
        //如果用户登录了,就正常加载tableView,否则就创建访客视图
        userlogin ? super.loadView() : setupVistorView()
    }
    
    /**
    创建访客视图
    */
    private func setupVistorView() {
        
     //设置view
        view = vistorView
        
        //设置代理
        vistorView.delegate = self
        

        if self is CZHomeViewController {
        //转轮转起来
            vistorView.rotationAnimation()
            //监听应用进入和退出后台
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "didEnterBackground", name: UIApplicationDidEnterBackgroundNotification, object: nil)
             NSNotificationCenter.defaultCenter().addObserver(self, selector: "didBecomeActive", name: UIApplicationDidBecomeActiveNotification, object: nil)
        
        }else if self is CZMessageViewController{
        
            vistorView.setupVistorView("visitordiscover_image_message", message: "登录后，别人评论你的微博，发给你的消息，都会在这里收到知")
        
        }else if self is CZDiscoverViewController{
            vistorView.setupVistorView("visitordiscover_image_message", message: "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过")
            
        }else if self is CZProfileViewController{
            
            vistorView.setupVistorView("visitordiscover_image_profile", message: "登录后，你的微博、相册、个人资料会显示在这里，展示给别人")
        }
        //设置导航栏按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: UIBarButtonItemStyle.Plain, target: self, action: "vistorViewRegisterClick")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: UIBarButtonItemStyle.Plain, target: self, action: "vistorViewLoginClick")
        
    }
    //应用被销毁时调用
    deinit {
    //移除通知
        if self is CZHomeViewController {
        
            NSNotificationCenter.defaultCenter().removeObserver(self)
        }
    }
    
    // 应用进入后台调用
    func didEnterBackground() {
    //暂停应用
        vistorView.pauseAnimation()
    
    }
    //进入应用时调用
    func didBecomeActive() {
    //开始核心动画
        vistorView.resumeAnmation()
    
    }
   
    //懒加载 访客视图
  private lazy var vistorView : CZVistorView = CZVistorView()
    
    
        
}
extension CZBaseViewController: CZVistorViewDelegate {
    //实现代理方法
    func vistorViewLoginClick() {
        //创建Oauth授权界面
        let oauthVC = CZOauthViewController()
        
       presentViewController(UINavigationController(rootViewController: oauthVC), animated: true, completion: nil)
    }
    func vistorViewRegisterClick() {
        
        print("注册按钮被点击了")
    }
    
}

